package com.xps.and.yuntong.Ui;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jude.utils.JUtils;
import com.xps.and.yuntong.Adapter.DingdanListAdpater;
import com.xps.and.yuntong.Data.bean1.OrderList;
import com.xps.and.yuntong.Data.net.UserNetWorks;
import com.xps.and.yuntong.R;
import com.xps.and.yuntong.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;

/**
 * Created by administrator on 2017-12-14.
 */

public class DingdanListActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_fanhui, iv_right;
    private TextView tv_middle;
    private PullToRefreshListView lv_dingdan_list;
    private DingdanListAdpater dingDanListAdapter;

    protected List<OrderList.ReturnBodyBean> adapterListData = new ArrayList();
    private int page = 1;

    @Override
    protected int getLayout() {
        return R.layout.activity_dingdan_list;
    }

    @Override
    protected void setData() {

        orderList();
        bindData();
    }

    @Override
    protected void init(Bundle inBundle) {
        initView();


    }

    private void initView() {
        iv_fanhui = (ImageView) findViewById(R.id.iv_fanhui);
        tv_middle = (TextView) findViewById(R.id.tv_middle);
        tv_middle.setText(R.string.dingdan_list);
        iv_right = (ImageView) findViewById(R.id.iv_right);
        lv_dingdan_list = (PullToRefreshListView) findViewById(R.id.lv_dingdan_list);

        iv_fanhui.setOnClickListener(this);
        iv_right.setOnClickListener(this);

        lv_dingdan_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(position>=1){
                    OrderList.ReturnBodyBean dataBean = (OrderList.ReturnBodyBean) dingDanListAdapter.getItem(position-1);
                    //状态后期需要修改
                    if (Integer.valueOf(dataBean.getOrder_state()) == 5) {

                        Intent intent = new Intent(DingdanListActivity.this, DingdanWanchengActivity.class);
                        intent.putExtra("driver_order_id",dataBean.getOrder_id());
                        startActivity(intent);
                    }
                }

            }
        });
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_fanhui:

                finish();
                break;
            case R.id.iv_right:


                break;


        }

    }

    private void orderList() {
        UserNetWorks.orderList("10", "1", new Subscriber<OrderList>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                JUtils.Toast("网络错误");
            }

            @Override
            public void onNext(OrderList orderList) {

                if (orderList.getReturn_code().equals("SUCCESS")) {

                    List<OrderList.ReturnBodyBean> list = orderList.getReturn_body();

                    adapterListData = list;
                    dingDanListAdapter = new DingdanListAdpater(DingdanListActivity.this, list);
                    if (list.size() > 5) {
                        lv_dingdan_list.setMode(PullToRefreshBase.Mode.BOTH);//两端刷新
                    } else {

                        lv_dingdan_list.setMode(PullToRefreshBase.Mode.PULL_FROM_START);//一端刷新

                    }
                    lv_dingdan_list.setAdapter(dingDanListAdapter);
                }
            }
        });


    }

    private void orderList(int page) {
        UserNetWorks.orderList("10", page + "", new Subscriber<OrderList>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                JUtils.Toast("网络错误");
            }

            @Override
            public void onNext(OrderList orderList) {

                if (orderList.getReturn_code().equals("SUCCESS")) {

                    List<OrderList.ReturnBodyBean> list = orderList.getReturn_body();
                    if (list != null && list.size() <= 0) {
                        dingDanListAdapter.notifyDataSetChanged();
                        JUtils.Toast("没有更多");
                        lv_dingdan_list.postDelayed(new Runnable() {

                            @Override
                            public void run() {
                                lv_dingdan_list.onRefreshComplete();
                            }
                        }, 1000);
                        return;
                    } else {

                        adapterListData = list;
                        dingDanListAdapter.notifyDataSetChanged();
                        lv_dingdan_list.postDelayed(new Runnable() {

                            @Override
                            public void run() {
                                lv_dingdan_list.onRefreshComplete();
                            }
                        }, 1000);
                    }
                }
            }
        });


    }

    private void bindData() {
        lv_dingdan_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                Log.e("111", "33" + "page" + page);
                page = 1;
                adapterListData.clear();
                orderList();
                dingDanListAdapter.notifyDataSetChanged();
                lv_dingdan_list.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        lv_dingdan_list.onRefreshComplete();
                    }
                }, 1000);

//                lv_pingjia1.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                Log.e("111", "33" + "page" + page);
                orderList(page);

            }
        });

    }

}
